package com.M.book.controller;

import com.M.book.constant.Constants;
import com.M.book.model.UserInfo;
import com.M.book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session) {
//        System.out.println(userName + " " + password);

        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }
//        System.out.println(userName + " " + password);
//        if("admin".equals(userName) && "admin".equals(password)) {
//            session.setAttribute("userName", userName);
//            return true;
//        }
        UserInfo userInfo = userService.getUserInfoByName(userName);
        if(userInfo == null || userInfo.getId() < 0) {
            return false;
        }
        if(password.equals(userInfo.getPassword())) {
            userInfo.setPassword("");
//            session.setAttribute("userName", userInfo);
            session.setAttribute(Constants.SESSION_USER_KEY, userInfo);
            return true;
        }
        return false;
    }
}
